<?php
/**
 * @Copyright   2008, XXXX-XXXX
 * All rights reserved. 
 * feature summary.
 * @filesource  BuyCommand.php 
 * @author      lipeng <lipengage@gmain.com> 
 * @package     / 
 * @version     $Id: $ 
 */

class BuyCommand extends command{
	public function excute() {
		$userdb = new user_dao();
		$activitydb = new activity_dao();

		$viewerid = $this->request['vid'];
		$container = $this->request['ctn'];
		$slaveid = $this->request['slaveid'];
		$nickname = $this->request['nickname'];

		if (empty($viewerid) || empty($slaveid) || empty($container)) {
			$this->set_messageid(command::$PARAM_ERROR);
			return true;
		}

		if ($nickname && strlen($nickname) > 20) {
			$nickname = substr($nickname, 0, 20);
		}

		$viewer = $userdb->user_get_byid($container, $viewerid);
		$slave = $userdb->user_get_byid($container, $slaveid);
		if ($viewer['count'] < 1 || $slave['count'] < 1) {
			$this->set_messageid(command::$PARAM_ERROR);
			return true;
		} 
		else {
			$viewer = $viewer['result'][0];
			$slave = $slave['result'][0];
		}
		
		//check if already has this slave
		if ($slave['master_id'] == $viewerid) {
			$this->set_messageid(command::$ALREADY_HAVE_SLAVE);
			return true;
		}
		if ($viewer['master_id'] == $slaveid) {
			$this->set_messageid(command::$CANT_BUY_MASTER);
			return true;
		}
		
		//check price and money
		$slave_price = $slave["price"];
		$viewer_cash = $viewer["cash"];
		if ($viewer_cash < $slave_price) {
			$this->set_messageid(command::$NOT_ENOUGH_MONEY);
			return true;
		}
		
		// check if slave is working.
		$activity = $activitydb->activity_get_user_last_activity($container, $slaveid);
		if ($activity['count'] > 0 && $activity['result'][0]["until"] > time()) {
		//if ($activity['count'] > 0 && $activity['result'][0]["until"] < time()) {
			$params = array();
			$params["slave"] = $slave["uid"];
			$parsms["work"] = $activity["result"][0]["command"];
			$params["until"] = $activity['result'][0]["until"]."000";
			$this->set_messageid(command::$SLAVE_AT_WORK);
			$this->set_params($params);
			return true;
		}

		// TODO: check buy limit per day

		// all check pass
		$this->set_haserror(false);

		if ($slave['master_id']) {
			$master = $userdb->user_get_byid($container, $slave['master_id']);
			$master = $master['result'][0];
			$new_activity = array();
			$new_activity["time"] = time;
			$new_activity["container"] = $container;
			$new_activity["command"] = command::$BUY_FROM_YOU;
			$new_activity["actor"] = $viewerid;
			$new_activity["actor_name"] = $viewer["name"];
			$new_activity["target"] = $slave["uid"];
			$new_activity["target_name"] = $slave["name"];
			$new_activity["type"] = activity_dao::$BUY;
			$activitydb->activity_insert($new_activity);
			$master["slave_num"] -= 1;
			$master["cash"] += $slave["price"];
			$userdb->user_update_byid($container, $master["uid"], $master);
		}
		$viewer["cash"] -= $slave["price"];
		$viewer["capital"] -= $slave["price"];
		$slave["capital"] -= $slave["price"];
		$slave["price"] = $slave["price"]*1.1;
		$slave["capital"] += $slave["price"];
		$viewer["capital"] += $slave["price"];
		$viewer["slave_num"] += 1;
		$slave["master_id"] = $viewer["uid"];
		$slave["master_name"] = $viewer["name"];

		$userdb->user_update_byid($container, $viewer["uid"], $viewer);
		$userdb->user_update_byid($container, $slave["uid"], $slave);

		$params = array();
		$params["viewer"] = $viewer;
		$params["slave"] = $slave;
		$params["profit"] = $slave;
		$new_activity = array();
		$new_activity["time"] = time();
		$new_activity["container"] = $container;
		$new_activity["actor"] = $viewer["uid"];
		$new_activity["actor_name"] = $viewer["name"];
		$new_activity["target"] = $slave["uid"];
		$new_activity["target_name"] = $slave["name"];
		$new_activity["command"] = command::$BUY;
		$activitydb->activity_insert($new_activity);
		$userdb->user_update_byid($container, $userid, $fields);
		$this->set_messageid(command::$BUY);
		$this->set_params($params);
		return false;
	}
}
?>